package com.bcxj.routeplan.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bcxj.internalcommon.constant.CommonStatusEnum;
import com.bcxj.internalcommon.entity.RouteRecord;
import com.bcxj.internalcommon.entity.Weather;
import com.bcxj.internalcommon.response.ResponseResult;
import com.bcxj.routeplan.client.WeatherClient;
import com.bcxj.routeplan.mapper.RouteRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

/**
 * ClassName: ReouteRecordService
 * Package: com.bcxj.routeplan.service
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/4/24 16:30
 * @Version 1.0
 * @Since 1.0
 */
@Service
public class RouteRecordService {
    @Autowired
    private RouteRecordMapper recordMapper;

    @Autowired
    private WeatherClient weatherClient;


    /**
     * 增添出行记录
     * @param record
     * @return
     */
    public ResponseResult add(RouteRecord record) {
        // 查询天气
        Weather weather = weatherClient.getWeather(record.getDepLongitude(), record.getDepLatitude());

        if(weather == null) {
            return ResponseResult.fail(CommonStatusEnum.GET_DATA_WEATHER);
        }
        // 补充数据
        LocalDateTime now = LocalDateTime.now();
        record.setCreateTime(now);
        record.setUpdateTime(now);
        record.setWeather(weather.getWeather());
        record.setTemperature(weather.getTemperature());

        // 增添数据
        int count = recordMapper.insert(record);

        return count == 1 ? ResponseResult.success() : ResponseResult.fail(CommonStatusEnum.FAIL);
    }


    /**
     * 查询用户的历史的出行记录
     * @param userId
     * @return
     */
    public ResponseResult list(Long userId) {
        return ResponseResult.success(recordMapper.selectList(new LambdaQueryWrapper<RouteRecord>().eq(RouteRecord::getUserId, userId)));
    }
}
